﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DentistOffice
{
    public partial class ZmienHaslo : System.Web.UI.Page
    {
        int patientId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["isLogged"] == null)
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                patientId = Int32.Parse(Session["isLogged"].ToString());
            } 
           // patientId = 44;
        }

        protected void updtPass_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;
            infoPlaceHolder.Visible = false;
            using (DentistEntities dB = new DentistEntities())
            {
                var query = dB.Patients.First(qr => qr.id == patientId);

                string oldPass = Crypt.GetMD5Hash(oldPassTextBox.Text);
                //string oldPass = oldPassTextBox.Text;
                if (query.password != oldPass)
                {
                    infoPlaceHolder.Visible = true;
                    infoLabel.Text = "Stare hasło się nie zgadza";
                    return;
                }

                query.password =  Crypt.GetMD5Hash(newPassTextBox.Text);
                dB.SaveChanges();
                infoPlaceHolder.Visible = true;
                infoLabel.Text = "Hasło uaktualnione";
                
                Page.Response.Redirect("~/MyAccount.aspx");
            }
        }

        protected void cncUpdt_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/HasloZmienione.aspx");
        }
    }
}